//
//  YSY_HeroDetail_ViewController.m
//  GoodManNews
//
//  Created by dllo on 16/1/12.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "YSY_HeroDetail_ViewController.h"
#import "YSY_HeroDetail_View.h"
#import "YSY_HeroDetail_CollectionViewCell.h"
#import "GoodManNews_AFNetTool.h"
#import "YSY_Game_HeroDetailModel.h"
#import "YSY_SkillDetail_ViewController.h"
#import "YSY_HeroVideo_ViewController.h"
#import "Masonry.h"
#import "YSY_Game_HeroModel.h"
#import "GS_DubDetail_ViewController.h"
#import "YSY_HeroIntroduce_ViewController.h"
#import "YSY_Player_ViewController.h"

#define HERODETAILURL(heroName) [NSString stringWithFormat:@"http://lolbox.duowan.com/phone/apiHeroDetail.php?heroName=%@", heroName]
#define HEROHEADIMAGE(heroName) [NSString stringWithFormat:@"http://img.lolbox.duowan.com/champions/%@_120x120.jpg", heroName]
@interface YSY_HeroDetail_ViewController ()<UICollectionViewDataSource, UICollectionViewDelegate, HeroIntroduceJumpToHeroDetail ,HeroVideoVCJumpToPlayerVC >
@property (nonatomic, retain) YSY_HeroDetail_View *heroDetailV;         //英雄头像等
@property (nonatomic, retain) UICollectionView *menuCollectionView;     //菜单选项
@property (nonatomic, retain) YSY_Game_HeroDetailModel *heroDetailModel;//英雄详情model
@property (nonatomic, retain) UIView *functionDetailV;                      //英雄详情页面

@property (nonatomic, retain) YSY_SkillDetail_ViewController *skillDetailVC; //英雄技能详情页面VC
@property (nonatomic, retain) YSY_HeroIntroduce_ViewController *heroIntroduceVC;//英雄关系VC
@property (nonatomic, retain) YSY_HeroVideo_ViewController *heroVideoVC; //英雄视频VC
@property (nonatomic, retain) GS_DubDetail_ViewController *dubDetailVC; //英雄配音详情VC
@property (nonatomic, retain) MBProgressHUD *progressHUD;       //菊花

@end

@implementation YSY_HeroDetail_ViewController

- (void)dealloc
{
    
    
    self.menuCollectionView.delegate = nil;
    self.menuCollectionView.dataSource = nil;
    
    [_progressHUD release];
    [_heroVideoVC release];
    [_skillDetailVC release];
    [_functionDetailV release];
    [_heroDetailModel release];
    [_heroDetailV release];
    [_menuCollectionView release];
    [_dubDetailVC release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    [self createView];
}
- (void)createView {
    
    [self createSubView];
    [self createFunctionCollectionView];
    [self createFunctionDetailView];
    [self getData];
    
}

#pragma mark - 数据
- (void)getData {
    
    self.progressHUD = [[MBProgressHUD alloc] initWithView:self.view];
    [self.view addSubview:self.progressHUD];
    self.progressHUD.labelText = @"Loading";
    [self.progressHUD show:YES];
    [self.progressHUD release];
    
    [GoodManNews_AFNetTool getNetWithURL:HERODETAILURL(self.heroEnName) body:nil headFile:nil responseStyle:WBNJSON success:^(NSURLSessionDataTask *task, id responseObject) {
        self.heroDetailModel = [[YSY_Game_HeroDetailModel alloc] init];
        [self.heroDetailModel  setValuesForKeysWithDictionary:responseObject];
        
        NSString *skill_B = [NSString stringWithFormat:@"%@_B",self.heroDetailModel.name];
        [self.heroDetailModel.skill_B setValuesForKeysWithDictionary:responseObject[skill_B]];
        NSString *skill_Q = [NSString stringWithFormat:@"%@_Q",self.heroDetailModel.name];
        [self.heroDetailModel.skill_Q setValuesForKeysWithDictionary:responseObject[skill_Q]];
        NSString *skill_W = [NSString stringWithFormat:@"%@_W",self.heroDetailModel.name];
        [self.heroDetailModel.skill_W setValuesForKeysWithDictionary:responseObject[skill_W]];
        NSString *skill_E = [NSString stringWithFormat:@"%@_E",self.heroDetailModel.name];
        [self.heroDetailModel.skill_E setValuesForKeysWithDictionary:responseObject[skill_E]];
        NSString *skill_R = [NSString stringWithFormat:@"%@_R",self.heroDetailModel.name];
        [self.heroDetailModel.skill_R setValuesForKeysWithDictionary:responseObject[skill_R]];
        
        [self setHeroDetailViewData:self.heroDetailModel];
        [self.progressHUD hide:YES];
        //创建功能子页
        [self createFuntionDetailSubviews];
        
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        
        
    }];
    
    
}

//英雄详情页面赋值
- (void)setHeroDetailViewData:(YSY_Game_HeroDetailModel *)model {
    [self.heroDetailV.headImage sd_setImageWithURL:[NSURL URLWithString:HEROHEADIMAGE(model.name)] placeholderImage:[UIImage imageNamed:@"zhanwei.png"]];
    self.heroDetailV.nameL.text = [NSString stringWithFormat:@"%@ %@", model.displayName, model.title];
    NSArray *arr = [model.price componentsSeparatedByString:@","];
    self.heroDetailV.priceL.text = [NSString stringWithFormat:@"金币:%@, 点券:%@",arr[0], arr[1]];
    if(model.tags.length > 0) {
        self.heroDetailV.locationL.text = model.tags;
    }else {
        self.heroDetailV.locationL.text = @"全能选手";
    }
    
    self.heroDetailV.baseL.text = [NSString stringWithFormat:@"攻:%@ 法:%@ 防:%@ 操作难度:%@",model.ratingAttack, model.ratingMagic, model.ratingDefense, model.ratingDifficulty];
}
#pragma mark - 界面
//创建英雄详情:头像等
- (void)createSubView {
    self.heroDetailV = [YSY_HeroDetail_View new];
    self.heroDetailV.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.heroDetailV];
    [self.heroDetailV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.and.right.equalTo(self.view);
        make.height.mas_equalTo(HEIGHT / 5);
    }];
    
}

//创建英雄其他数据功能
- (void)createFunctionCollectionView {
    UICollectionViewFlowLayout *flowLayout = [UICollectionViewFlowLayout new];
    flowLayout.itemSize = CGSizeMake(WIDTH / 5, HEIGHT / 20);
    flowLayout.sectionInset = UIEdgeInsetsMake(PAD, PAD, PAD, PAD);
    
    self.menuCollectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:flowLayout];
    [self.view addSubview:self.menuCollectionView];
    [self.menuCollectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.heroDetailV.mas_bottom);
        make.left.right.equalTo(self.view);
        make.height.mas_equalTo(HEIGHT / 12);
    }];
    self.menuCollectionView.delegate = self;
    self.menuCollectionView.dataSource = self;
    [self.menuCollectionView registerClass:[YSY_HeroDetail_CollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
    [_menuCollectionView release];
}
/**
 *  功能详情页面
 */
- (void)createFunctionDetailView {
    self.functionDetailV = [UIView new];
    self.functionDetailV.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.functionDetailV];
    [self.functionDetailV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.menuCollectionView.mas_bottom);
        make.left.right.equalTo(self.view);
        make.height.mas_equalTo((HEIGHT - HEIGHT / 12 - HEIGHT / 5 - 64 - 49 ) );
    }];
    
}

//创建功能子页:添加到self.functionDetailV 上
- (void)createFuntionDetailSubviews {

    //相关视频
    self.heroVideoVC = [YSY_HeroVideo_ViewController new];
    self.heroVideoVC.delegate = self;
    [self.functionDetailV addSubview:self.heroVideoVC.view];
    [self.heroVideoVC.view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.functionDetailV);

    }];
    //配音详情页面
    self.dubDetailVC = [GS_DubDetail_ViewController new];
    [self.functionDetailV addSubview:self.dubDetailVC.view];
    [self.dubDetailVC.view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.functionDetailV);
        

    }];
    //英雄关系页面
    self.heroIntroduceVC = [YSY_HeroIntroduce_ViewController new];
    self.heroIntroduceVC.delegata = self;
    [self.functionDetailV addSubview:self.heroIntroduceVC.view];
    [self.heroIntroduceVC.view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.functionDetailV);
    }];

    //技能详情页面
    self.skillDetailVC = [YSY_SkillDetail_ViewController new];
    [self.functionDetailV addSubview:self.skillDetailVC.view];
    [self.skillDetailVC addDataToSkillDetailHeadView:self.heroDetailModel];

    [self.skillDetailVC.view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.functionDetailV);
    }];
    
    
    
    
}
     
#pragma mark - UICollectionView协议方法
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return 4;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    YSY_HeroDetail_CollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    switch (indexPath.row) {
        case 0:
            cell.menuL.text = @"技能";
            cell.bg_image.backgroundColor = [UIColor redColor];
            cell.menuL.textColor = [UIColor blackColor];
            break;
        case 1:
            cell.menuL.text = @"关系";
            break;
        case 2:
            cell.menuL.text = @"视频";
            break;
        case 3:
            cell.menuL.text = @"配音";
            break;
        default:
            break;
    }
    return cell;
}
//功能 被选中
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
    NSMutableArray *arr = [NSMutableArray arrayWithArray:collectionView.visibleCells];
    for (YSY_HeroDetail_CollectionViewCell *tempCell in arr) {
        tempCell.bg_image.backgroundColor = [UIColor grayColor];
        tempCell.menuL.textColor = [UIColor whiteColor];
    }
    YSY_HeroDetail_CollectionViewCell *cell = (YSY_HeroDetail_CollectionViewCell *)[collectionView cellForItemAtIndexPath:indexPath];
    cell.bg_image.backgroundColor = [UIColor redColor];
    cell.menuL.textColor = [UIColor blackColor];
    
    switch (indexPath.row) {
        case 0:
            //传值给配音界面
            [self.dubDetailVC getNum:0];
            //把选择的页面提前
            [self.functionDetailV bringSubviewToFront:self.skillDetailVC.view];
            break;
        case 1:

            self.heroIntroduceVC.heroModel = self.heroDetailModel;
            [self.heroIntroduceVC.myTableV reloadData];
            [self.functionDetailV bringSubviewToFront:self.heroIntroduceVC.view];
            [self.dubDetailVC getNum:0];

            break;
        case 2:
            [self.dubDetailVC getNum:0];
            self.heroVideoVC.heroName = self.heroDetailModel.name;
            [self.heroVideoVC getData];
            [self.functionDetailV bringSubviewToFront:self.heroVideoVC.view];
            break;
        case 3:
            
            [self.dubDetailVC getNum:3];
            
            [self.functionDetailV bringSubviewToFront:self.dubDetailVC.view];
            self.dubDetailVC.heroName = self.heroDetailModel.name;
            [self.dubDetailVC getData];
            break;
            
        default:
            break;
    }
    
}

#pragma mark - YSY_HeroIntroduce_ViewController协议方法
- (void)jumpToHeroDetail:(NSString *)enName cnName:(NSString *)cnName {
    YSY_HeroDetail_ViewController *heroVC = [[YSY_HeroDetail_ViewController alloc] init];
    heroVC.heroEnName = enName;
    heroVC.navigationItem.title = [self getcnNameByenName:enName];
    [self.navigationController pushViewController:heroVC animated:YES];

}
#pragma mark -YSY_HeroVideo_ViewController
- (void)jumpToPlayerVC:(NSString *)videoId {

    YSY_Player_ViewController *playerVC = [[YSY_Player_ViewController alloc] init];
    playerVC.videlVid = videoId;
    NSLog(@"++++++:%@", videoId);
    [self.navigationController pushViewController:playerVC animated:YES];

}

#pragma mark - 私有方法
//通过英文名字找中文名字
- (NSString *)getcnNameByenName:(NSString *)enName {
    NSString *cnName = nil;
    NSMutableArray *arr = [NSMutableArray arrayWithArray:[YSY_HeroArr shareHeroArr].heroArr];
     
    for (YSY_Game_HeroModel *model in arr) {
       
        if ([model.enName isEqualToString:enName]) {
            cnName = model.cnName;
        }
    }
    return cnName;
}









- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end


